﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FbKPI.Model;
using FbKPI.Facade;
using System.Globalization;
using System.Transactions;
using Newtonsoft.Json.Linq;
using System.Text;

namespace FBKPI.ProcessRequest
{
    /// <summary>
    /// Summary description for KPIAssignmentHandler
    /// </summary>
    public class KPIAssignmentHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string MethodName = string.Empty;

            MethodName = context.Request.Params["method"];

            if (MethodName == "Save")
            {
                context.Response.ContentType = "text/plain";
                string msg = this.Save();
                context.Response.Write(msg);
                //context.Response.Write("Hello World");
            }


            if (MethodName == "GroupAssigmentByKpiID")
            {
                int KpiID = Convert.ToInt32(context.Request.Params["KpiID"].ToString());
                KPIAssignmentFaced fd = new KPIAssignmentFaced();
                List<KPIAssignmentBy> list = fd.GetByKpiAssignmentByID(KpiID);
                var result = list;
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));

            }

            if (MethodName == "GroupAssigmentByKpiIDDua")
            {
                int KpiID = Convert.ToInt32(context.Request.Params["KpiID"].ToString());
                KPIAssignmentFaced fd = new KPIAssignmentFaced();
                List<KPIAssignmentBy> list = fd.GetByKpiAssignmentByIDDua(KpiID);
                var result = list;
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));

            }

            if (MethodName == "GroupByKpiID")
            {
                int KpiID = Convert.ToInt32(context.Request.Params["KpiID"].ToString());
                KPIAssignmentFaced fd = new KPIAssignmentFaced();
                List<KPIAssignment> list = fd.GetByKpiID(KpiID);
                var result = list;
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));

            }

            if (MethodName == "GroupByKpiIDRgmType")
            {
                int KpiID = Convert.ToInt32(context.Request.Params["KpiID"].ToString());
                KPIAssignmentFaced fd = new KPIAssignmentFaced();
                List<KPIAssignmentRgmType> list = fd.GetByKpiIDRgmType(KpiID);
                var result = list;
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }

            if (MethodName == "GroupByKpiIDRgm")
            {
                int KpiID = Convert.ToInt32(context.Request.Params["KpiID"].ToString());
                KPIAssignmentFaced fd = new KPIAssignmentFaced();
                List<KPIAssignmentRgm> list = fd.GetByKpiIDRgm(KpiID);
                var result = list;
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }

            if (MethodName == "GroupByKpiIDBranch")
            {
                int KpiID = Convert.ToInt32(context.Request.Params["KpiID"].ToString());
                KPIAssignmentFaced fd = new KPIAssignmentFaced();
                List<KPIAssignmentBranch> list = fd.GetByKpiIDBranch(KpiID);
                var result = list;
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string Save()
        {

            //DateTime createdDate = Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy"), CultureInfo.GetCultureInfo("en-US").DateTimeFormat);

            //string result = "Transaction Failed";
            //string groupID = HttpContext.Current.Request.QueryString["groupID"];
            //int KpiID = Convert.ToInt32(HttpContext.Current.Request.QueryString["ID"]);
            //Employee CurrUser = new EmployeeFacade().GetEmployeeByID(Convert.ToInt32(HttpContext.Current.Request.QueryString["CurrUser"]));
            //List<KPIAssignment> listGroup = new List<KPIAssignment>();
            //if (groupID != "")
            //{
            //    groupID = groupID.Substring(0, groupID.Length - 1);
            //    //string user = "";
                
            //    string[] arrGroupID = groupID.Split('|');
            //    KPIAssignment objGroup = new KPIAssignment();
            //    foreach (string data in arrGroupID)
            //    {
            //        objGroup = new KPIAssignment();
            //        objGroup.KpiID = KpiID;
            //        objGroup.GroupID = Convert.ToInt32(data);
            //        objGroup.CreateBy = CurrUser.Email;
            //        objGroup.CreateDate = DateTime.Now;

            //        listGroup.Add(objGroup);
            //    }
            //}


            //string RgmTypeID = HttpContext.Current.Request.QueryString["RgmTypeID"];
            //List<KPIAssignmentRgmType> listRgmType = new List<KPIAssignmentRgmType>();
            //if (RgmTypeID != "")
            //{
            //    RgmTypeID = RgmTypeID.Substring(0, RgmTypeID.Length - 1);
            //    string[] arrRgmTypeID = RgmTypeID.Split('|');
            //    KPIAssignmentRgmType objRgmType = new KPIAssignmentRgmType();
            //    foreach (string data in arrRgmTypeID)
            //    {
            //        objRgmType = new KPIAssignmentRgmType();
            //        objRgmType.KpiID = KpiID;
            //        objRgmType.RgmTypeID = Convert.ToInt32(data);
            //        objRgmType.CreateBy = CurrUser.Email;
            //        objRgmType.CreateDate = DateTime.Now;

            //        listRgmType.Add(objRgmType);
            //    }
            //}

            //string RgmID = HttpContext.Current.Request.QueryString["RgmID"];
            //List<KPIAssignmentRgm> listRgm = new List<KPIAssignmentRgm>();
            //if (RgmID != "")
            //{
            //    RgmID = RgmID.Substring(0, RgmID.Length - 1);
            //    string[] arrRgmID = RgmID.Split('|');
            //    KPIAssignmentRgm objRgm = new KPIAssignmentRgm();
            //    foreach (string data in arrRgmID)
            //    {
            //        objRgm = new KPIAssignmentRgm();
            //        objRgm.KpiID = KpiID;
            //        objRgm.RgmID = Convert.ToInt32(data);
            //        objRgm.CreateBy = CurrUser.Email;
            //        objRgm.CreateDate = DateTime.Now;

            //        listRgm.Add(objRgm);
            //    }
            //}

            //string BranchID = HttpContext.Current.Request.QueryString["BranchID"];
            //List<KPIAssignmentBranch> listBranch = new List<KPIAssignmentBranch>();
            //if (BranchID != "")
            //{
            //    BranchID = BranchID.Substring(0, BranchID.Length - 1);
            //    string[] arrBranchID = BranchID.Split('|');
            //    KPIAssignmentBranch objBranch = new KPIAssignmentBranch();
            //    foreach (string data in arrBranchID)
            //    {
            //        objBranch = new KPIAssignmentBranch();
            //        objBranch.KpiID = KpiID;
            //        objBranch.BranchID = Convert.ToInt32(data);
            //        objBranch.CreateBy = CurrUser.Email;
            //        objBranch.CreateDate = DateTime.Now;

            //        listBranch.Add(objBranch);
            //    }
            //}


            //    KPIAssignmentFaced df = new KPIAssignmentFaced();
            //    if (df.Save(KpiID,listGroup,listRgmType,listRgm,listBranch))
            //    {
            //        result = "Transaction Success";
            //    }
            //    else
            //    {
            //        result = "Transaction Failed";
            //    }


            string result = "Transaction Failed";
            string groupID = HttpContext.Current.Request.QueryString["groupID"];
            string IdAssignKPI = HttpContext.Current.Request.QueryString["IdAssignKPI"];
            int KpiID = Convert.ToInt32(HttpContext.Current.Request.QueryString["ID"]);
            Employee CurrUser = new EmployeeFacade().GetEmployeeByID(Convert.ToInt32(HttpContext.Current.Request.QueryString["CurrUser"]));
            List<KPIAssignmentBy> listGroup = new List<KPIAssignmentBy>();
            KPIAssignmentBy objGroup = new KPIAssignmentBy();
            if (IdAssignKPI != "")
            {
                IdAssignKPI = IdAssignKPI.Substring(0, groupID.Length - 1);
                //string user = "";

                string[] arrIdAssignKPI = groupID.Split('|');
                
                foreach (string data in arrIdAssignKPI)
                {
                    objGroup = new KPIAssignmentBy();
                    objGroup.KpiID = KpiID;
                    objGroup.GroupID = Convert.ToInt32(groupID);
                    objGroup.IdAssignKPI = Convert.ToInt32(data);
                    objGroup.CreateBy = CurrUser.Email;
                    objGroup.CreateDate = DateTime.Now;

                    listGroup.Add(objGroup);
                }
            }

            string IdAssignKPIDua = HttpContext.Current.Request.QueryString["IdAssignKPIDua"];
            //List<KPIAssignmentBranch> listBranch = new List<KPIAssignmentBranch>();
            if (IdAssignKPIDua != "")
            {
                IdAssignKPIDua = IdAssignKPIDua.Substring(0, IdAssignKPIDua.Length - 1);
                string[] arrIdAssignKPIDua = IdAssignKPIDua.Split('|');
                string groupIDDua = HttpContext.Current.Request.QueryString["groupIDDua"];
                //KPIAssignmentBranch objBranch = new KPIAssignmentBranch();
                foreach (string data in arrIdAssignKPIDua)
                {
                    objGroup = new KPIAssignmentBy();
                    objGroup.KpiID = KpiID;
                    objGroup.GroupID = Convert.ToInt32(groupIDDua);
                    objGroup.IdAssignKPI = Convert.ToInt32(data);
                    objGroup.CreateBy = CurrUser.Email;
                    objGroup.CreateDate = DateTime.Now;

                    listGroup.Add(objGroup);
                }
            }

            KPIAssignmentFaced df = new KPIAssignmentFaced();
            if (df.Save(KpiID, listGroup))
            {
                result = "Transaction Success";
            }
            else
            {
                result = "Transaction Failed";
            }
           

            return result;
        }
    }
}